/**
 * 
 */
package com.alstudio.utils.absListView.loadmore;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.utils.log.ALLog;
import com.loovee.imaohu.R;

/**
 * 为abslist类的列表控制器加入一个footview，并控制在滑动到底部时，通过回调OnLoadMoreListener通知ui，开始执行加载更多
 * 
 * @author alonso lee
 * 
 */
public class AbsListViewLoadMoreManager implements OnScrollListener {

	/**
	 * Listener that will receive notifications every time the list scrolls.
	 */
	private OnScrollListener mOnScrollListener;
	private LayoutInflater mInflater;

	// footer view
	private RelativeLayout mFooterView;
	// private TextView mLabLoadMore;
	private ProgressBar mProgressBarLoadMore;

	// Listener to process load more items when user reaches the end of the list
	private OnLoadMoreListener mOnLoadMoreListener;
	// To know if the list is loading more items
	private boolean mIsLoadingMore = false;
	private int mCurrentScrollState;

	private Context context;

	/**
	 * 
	 */
	public AbsListViewLoadMoreManager(AbsListView listview, Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		init(context, listview);
	}

	private void init(Context context, AbsListView listview) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// footer
		mFooterView = (RelativeLayout) mInflater.inflate(
				R.layout.load_more_footer, null);
		/*
		 * mLabLoadMore = (TextView) mFooterView
		 * .findViewById(R.id.load_more_lab_view);
		 */
		mProgressBarLoadMore = (ProgressBar) mFooterView
				.findViewById(R.id.load_more_progressBar);
		listview.setOnScrollListener(this);

		if (listview instanceof ListView) {
			((ListView) listview).addFooterView(mFooterView);
		}

	}

	public View getFootView() {
		return mFooterView;
	}

	/**
	 * 设置滚蛋监听器
	 * 
	 * @param mOnScrollListener
	 */
	public void setOnScrollListener(OnScrollListener mOnScrollListener) {
		this.mOnScrollListener = mOnScrollListener;
	}

	/**
	 * Register a callback to be invoked when this list reaches the end (last
	 * item be visible)
	 * 
	 * @param onLoadMoreListener
	 *            The callback to run.
	 */

	public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
		mOnLoadMoreListener = onLoadMoreListener;
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if (mOnScrollListener != null) {
			mOnScrollListener.onScroll(view, firstVisibleItem,
					visibleItemCount, totalItemCount);
		}

		if (mOnLoadMoreListener != null) {

			if (visibleItemCount == totalItemCount) {
				mProgressBarLoadMore.setVisibility(View.GONE);
				// mLabLoadMore.setVisibility(View.GONE);
				return;
			}

			boolean loadMore = firstVisibleItem + visibleItemCount >= totalItemCount;

			if (!mIsLoadingMore && loadMore
					&& mCurrentScrollState != SCROLL_STATE_IDLE) {
				mProgressBarLoadMore.setVisibility(View.VISIBLE);
				// mLabLoadMore.setVisibility(View.VISIBLE);
				mIsLoadingMore = true;
				onLoadMore();
			}

		}

	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {

		// bug fix: listview was not clickable after scroll
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
			view.invalidateViews();
		}

		switch (scrollState) {
		case OnScrollListener.SCROLL_STATE_IDLE:
			ALLocalEnv.getLocalEnv().getImageLoader().resume();
			break;
		case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
			ALLocalEnv.getLocalEnv().getImageLoader().pause();
			break;
		case OnScrollListener.SCROLL_STATE_FLING:
			ALLocalEnv.getLocalEnv().getImageLoader().pause();
			break;
		}

		mCurrentScrollState = scrollState;

		if (mOnScrollListener != null) {
			mOnScrollListener.onScrollStateChanged(view, scrollState);
		}

	}

	public void onLoadMore() {
		mProgressBarLoadMore.setVisibility(View.VISIBLE);
		if (mOnLoadMoreListener != null) {
			mOnLoadMoreListener.onLoadMore();
		}
	}

	/**
	 * Notify the loading more operation has finished
	 */
	public void onLoadMoreComplete() {
		mIsLoadingMore = false;
		mProgressBarLoadMore.setVisibility(View.GONE);
	}

}
